﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Demo.Services.Contracts.DTO.Infos;

namespace Demo.Web.Catalogs
{
    public partial class ProjectArea : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
            }
        }

        public void ShowWindow()
        {

            AreaWindow.Show();
            List<AreaInfo> assignedAreaList = null;
            List<AreaInfo> unAssignedAreaList = null;

            if (Session["AssignedAreas"] != null)
            {
                assignedAreaList = (List<AreaInfo>)Session["AssignedAreas"];
                gpAssigned.GetStore().DataSource = assignedAreaList;
                gpAssigned.GetStore().DataBind();
            }
            if (Session["UnAssignedAreas"] != null)
            {
                unAssignedAreaList = (List<AreaInfo>)Session["UnAssignedAreas"];                                                    
                gpUnAssigned.GetStore().DataSource = unAssignedAreaList;
                gpUnAssigned.GetStore().DataBind();
            }
        }

        protected void Asignar(object sender, DirectEventArgs e)
        {
            string jsonA = e.ExtraParams["Assigned"];
            string jsonUn = e.ExtraParams["UnAssigned"];

            SetTecnologies("AssignedAreas", jsonA);
            SetTecnologies("UnAssignedAreas", jsonUn);
            AreaWindow.Close();
        }

        private void SetTecnologies(string session, string json)
        {
            List<AreaInfo> areaList = new List<AreaInfo>();
            Dictionary<string, string>[] areas = JSON.Deserialize<Dictionary<string, string>[]>(json);
            foreach (Dictionary<string, string> row in areas)
            {
                string desc = row["Description"];
                int id = Convert.ToInt32(row["Id"]);
                AreaInfo tecnology = new AreaInfo();
                tecnology.Description = desc;
                tecnology.Id = id;
                areaList.Add(tecnology);
            }
            Session[session] = areaList;
        }
    }
}